package org.jeecg.common.util.app;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.util.UUID;

@Slf4j
public class CommonUtil {
    /**
     * 生成6位数字验证码
     * @return
     */
    public static int generatorSixCode(){
        return (int)((Math.random()*9+1)*100000);
    }

    /**
     * MD5加密数据
     * @param data
     * @param salt
     * @return
     */
    public static String encryptData(String data, String salt){
        return String.valueOf(new SimpleHash("MD5", data,  salt, 1024));
    }

    /**
     * 密码盐加密密码
     * @param salt
     * @param pwd
     * @return
     */
    public static String getEncryptionPwd(String salt,String pwd){
        String encryptPwd = "";

        String hashAlgorithmName = "md5";
        int hashIterations = 1024;

        ByteSource credentialsSalt = ByteSource.Util.bytes(salt);
        encryptPwd = new SimpleHash(hashAlgorithmName, pwd, credentialsSalt, hashIterations).toHex();

        return encryptPwd;
    }

    /**
     * 生成密码盐
     * @return
     */
    public static String getSalt(){
        return UUID.randomUUID().toString().replace("-","");
    }

    /**
     * 生成 ID_NUMBER
     * @param serialNumber
     * @param len
     * @return
     */
    public static String generatorIdNum(int serialNumber,int len){
        String idNum = "";
        idNum = String.valueOf(serialNumber);
        while(idNum.length()<len){
            idNum="0"+idNum;
        }
        return "ID:"+idNum;
    }

}
